package com.project.demo.controller;

import com.project.demo.entity.ClothingMall;
import com.project.demo.service.ClothingMallService;
import com.project.demo.controller.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.project.demo.constant.FindConfig;
import org.springframework.util.StringUtils;
import com.project.demo.service.GoodsService;
import java.util.stream.Collectors;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;


/**
 * 服装商城：(ClothingMall)表控制层
 *
 */
@RestController
@RequestMapping("/clothing_mall")
public class ClothingMallController extends BaseController<ClothingMall, ClothingMallService> {

    /**
     * 服装商城对象
     */
    @Autowired
    public ClothingMallController(ClothingMallService service) {
        setService(service);
    }

    @Autowired
    private GoodsService goodsService;


    @PostMapping("/add")
    @Transactional
    public Map<String, Object> add(HttpServletRequest request) throws IOException {
        Map<String,Object> paramMap = service.readBody(request.getReader());
        this.addMap(paramMap);
        return success(1);
    }

    /**
     * 标签推荐
     */
    @GetMapping("/get_tag_list")
    @Transactional
    public Map<String, Object> getTagList(HttpServletRequest request) {
        Map<String,String> paramMap = service.readQuery(request);
        Map<String,String> configMap = service.readConfig(request);
        configMap.computeIfAbsent(FindConfig.PAGE, k -> String.valueOf(1));
        configMap.computeIfAbsent(FindConfig.SIZE, k -> String.valueOf(1));
            configMap.put(FindConfig.ORDER_BY, "goods_id asc");
        if (paramMap.get("user_id") == null || paramMap.get("user_id").equals("")) {
            List goodsList = goodsService.selectBaseList(goodsService.select(paramMap, configMap));
            Map<String, Object> map = new HashMap<>();
            map.put("list", goodsList);
            map.put("count", goodsList.size());
            return success(map);
        } else {
                                String tagField = "clothing_specifications";
                                                    Map<String, String> tagFieldMap = new HashMap<>();
                                                                                                                                            tagFieldMap.put("普通用户", "recommended_specifications");
                                                                                                                                                    String userTableSql = "SELECT t2.source_table,t1.user_group FROM `user` t1 LEFT JOIN user_group t2 ON t1.user_group = t2.`name` WHERE user_id = " + paramMap.get("user_id");
            List<Map<String, Object>> userTable = service.selectMapBaseList(userTableSql);
            //数据错误直接返回list
            if (userTable == null || userTable.size() <= 0) {
                List goodsList = goodsService.selectBaseList(goodsService.select(paramMap, configMap));
                Map<String, Object> map = new HashMap<>();
                map.put("list", goodsList);
                map.put("count", goodsList.size());
                return success(map);
            } else {
                String userTagField = tagFieldMap.get(String.valueOf(userTable.get(0).get("user_group")));
                String userSql = "SELECT * FROM " + userTable.get(0).get("source_table") + " WHERE user_id = " + paramMap.get("user_id");
                List<Map<String, Object>> user = service.selectMapBaseList(userSql);
                if (user == null || user.size() <= 0 || StringUtils.isEmpty(String.valueOf(user.get(0).get(userTagField)))) {
                    List goodsList = goodsService.selectBaseList(goodsService.select(paramMap, configMap));
                    Map<String, Object> map = new HashMap<>();
                    map.put("list", goodsList);
                    map.put("count", goodsList.size());
                    return success(map);
                } else {
                    String[] tags = String.valueOf(user.get(0).get(userTagField)).split(",");
                    if (tags.length <= 0) {
                        List goodsList = goodsService.selectBaseList(goodsService.select(paramMap, configMap));
                        Map<String, Object> map = new HashMap<>();
                        map.put("list", goodsList);
                        map.put("count", goodsList.size());
                        return success(map);
                    } else {
                        List<Integer> idArr = new ArrayList<>();
                        for (int i = 0; i < tags.length; i++) {
                            String productSql = "SELECT * FROM clothing_mall WHERE " + tagField + " = '" + tags[i] + "'";
                            List<Map<String, Object>> product = service.selectMapBaseList(productSql);
                            if (product!=null && product.size() > 0) {
                                for (int j = 0; j < product.size(); j++) {
                                    idArr.add(Integer.parseInt(String.valueOf(product.get(j).get("clothing_mall_id"))));
                                }
                            }

                        }
                        if (idArr.size() > 0) {
                            String goodsSql = "SELECT * FROM goods WHERE source_table = 'clothing_mall' and source_id in (" + idArr.stream().map(String::valueOf).collect(Collectors.joining(",")) + ")";
                            List<Map<String, Object>> goods = service.selectMapBaseList(goodsSql);
                            if (goods!=null && goods.size() < Integer.parseInt(configMap.get(FindConfig.SIZE))) {
                                String otherGoodsSql = "SELECT * FROM goods WHERE source_table = 'clothing_mall' and source_id not in (" + idArr.stream().map(String::valueOf).collect(Collectors.joining(",")) + ") ORDER BY goods_id ASC LIMIT 0," + (Integer.parseInt(configMap.get(FindConfig.SIZE)) - goods.size());
                                List<Map<String, Object>> otherGoodsList = service.selectMapBaseList(otherGoodsSql);
                                if (otherGoodsList!=null && otherGoodsList.size() > 0) {
                                    goods.addAll(otherGoodsList);
                                }
                                Map<String, Object> map = new HashMap<>();
                                map.put("list", goods);
                                map.put("count", goods.size());
                                return success(map);
                            }
                            List goodsList = goodsService.selectBaseList(goodsService.select(paramMap, configMap));
                            Map<String, Object> map = new HashMap<>();
                            map.put("list", goodsList);
                            map.put("count", goodsList.size());
                            return success(map);
                        } else {
                            List goodsList = goodsService.selectBaseList(goodsService.select(paramMap, configMap));
                            Map<String, Object> map = new HashMap<>();
                            map.put("list", goodsList);
                            map.put("count", goodsList.size());
                            return success(map);
                        }
                    }
                }
            }
        }
        }

}
